//
//  LoginViewController.m
//  YiLiao
//
//  Created by Infinite on 16/7/1.
//  Copyright © 2016年 Infinite. All rights reserved.
//

#import <AFNetworking.h>
#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "TabbarViewController.h"




@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *UserName;
@property (weak, nonatomic) IBOutlet UITextField *PassWord;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //将导航条给本视图控制器
    UINavigationController * NAVC = [[UINavigationController alloc]initWithRootViewController:self];
    [UIApplication sharedApplication].delegate.window.rootViewController=NAVC;
    
    self.title= @"登入";
    
    NSString * accessToken = [[NSUserDefaults standardUserDefaults]objectForKey:@"access_token"];
    NSDate * time = [[NSUserDefaults standardUserDefaults]objectForKey:@"time"];
    
    if (accessToken!=nil &&time>=0) {
        NSLog(@"已经登入");
        TabbarViewController * tabbarVC = [[TabbarViewController alloc]init];
        [self.navigationController pushViewController:tabbarVC animated:YES];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)loginAction:(UIButton *)sender {
    
    [self AFNetSession];
    

    
}
-(void)AFNetSession{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/html",nil]];
    
    NSDictionary * parameters =  @{@"command":@"ST_L",@"name":self.UserName.text,@"psw":self.PassWord.text};
    [manager POST:@"http://localhost:8080/st/s" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"json:%@",responseObject);
        
        
        
        
        NSDictionary *infoDic = responseObject;
        if ([[infoDic objectForKey:@"result"] isEqualToString:@"0"]) {
            NSLog(@"密码错误");
        }else if ([[infoDic objectForKey:@"result"] isEqualToString:@"1"]){
            NSLog(@"成功登入");
            TabbarViewController * tabbarVC = [[TabbarViewController alloc]init];
            [self.navigationController pushViewController:tabbarVC animated:YES];
            //            UINavigationController * navc = [[UINavigationController alloc]initWithRootViewController:tabbarVC];
            //            [UIApplication sharedApplication].delegate.window.rootViewController=navc
            
            NSUserDefaults * userDefult = [NSUserDefaults standardUserDefaults];
            NSString * accessToken = [infoDic objectForKey:@"access_token"];
            [userDefult setObject:accessToken forKey:@"access_token"];
            
            NSDate * timeDate =[NSDate dateWithTimeIntervalSinceNow:[infoDic[@"time"]longLongValue]];
            [userDefult setObject:timeDate forKey:@"time"];
            
            
            
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];
}
- (IBAction)RegisterAction:(UIButton *)sender {
    RegisterViewController * registerVC = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
